<template>
  <div class="app-container">
    <!-- 查询行 -->
    <el-row :gutter="10" class="mb8 query-row">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="编码" prop="code">
          <el-input v-model="queryParams.code" placeholder="请输入编码" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入名称" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="queryParams.memo" placeholder="请输入备注" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 按钮行 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['aitcommon:zjkconfig:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['aitcommon:zjkconfig:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['aitcommon:zjkconfig:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
          @click="handleExport" v-hasPermi="['aitcommon:zjkconfig:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="zjkconfigList" @selection-change="handleSelectionChange"
      @row-dblclick="handleUpdate" :row-style="{ height: '32px' }" :cell-style="{ padding: '0px' }" border
      :height="tableHeight">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" width="55" align="center" prop="id" />
      <el-table-column label="编码" width="120" align="center" prop="code" show-overflow-tooltip />
      <el-table-column label="名称" width="220" align="center" prop="name" show-overflow-tooltip />
      <el-table-column label="sql配置" width="220" align="center" prop="sqlConf" show-overflow-tooltip />
      <el-table-column label="数据配置" width="260" align="center" prop="dbConf" show-overflow-tooltip />
      <el-table-column label="是否生效" width="80" align="center" prop="isEnable" :formatter="boolFormat" />
      <el-table-column label="备注" width="120" align="center" prop="memo" show-overflow-tooltip />
      <el-table-column label="启用流程" width="80" align="center" prop="enableFlow" :formatter="boolFormat" />
      <el-table-column label="流程类型" width="280" align="center" prop="flowType" show-overflow-tooltip />
      <el-table-column label="编码字段" width="120" align="center" prop="codeField" show-overflow-tooltip />
      <el-table-column label="启用更新" width="80" align="center" prop="enableUpdate" :formatter="boolFormat" />
      <el-table-column label="更新表名" width="280" align="center" prop="updateTable" show-overflow-tooltip />
      <el-table-column label="更新主键" width="120" align="center" prop="updateKey" show-overflow-tooltip />

      <el-table-column label="操作" width="120" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['aitcommon:zjkconfig:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['aitcommon:zjkconfig:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改中间库配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-divider content-position="left">基本信息</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-tooltip content="配置文件中的SQL参数,如:sysflow.AITEntityError" placement="top-start" effect="dark">
            <el-form-item label="sql配置" prop="sqlConf">
              <el-input v-model="form.sqlConf" placeholder="请输入sql配置" />
            </el-form-item>
            </el-tooltip>
          </el-col>
          <el-col :span="12">
            <el-tooltip content="配置文件中的DB参数,如:config.aitconfig.datasource.aitdb" placement="top-start" effect="dark">
            <el-form-item label="数据配置" prop="dbConf">
              <el-input v-model="form.dbConf" placeholder="请输入数据库配置" />
            </el-form-item>
            </el-tooltip>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否生效" prop="isEnable">
              <el-switch v-model="form.isEnable" :active-value="1" :inactive-value="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="memo">
              <el-input v-model="form.memo" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">流程相关</el-divider>
        <el-row>
          <el-col :span="12">
            <el-tooltip content="启用流程后,可以在中间库查询界面进行手工同步" placement="top-start" effect="dark">
            <el-form-item label="启用流程" prop="enableFlow">
              <el-switch v-model="form.enableFlow" :active-value="1" :inactive-value="0"/>
            </el-form-item>
            </el-tooltip>
          </el-col>
          <el-col :span="12">
            <el-tooltip content="中间库对应的流程编码,支持${}取数据中的字段" placement="top-start" effect="dark">
            <el-form-item label="流程类型" prop="flowType">
              <el-input v-model="form.flowType" placeholder="请输入" :disabled="form.enableFlow==0"/>
            </el-form-item>
            </el-tooltip>
          </el-col>
          <el-col :span="12">
            <el-tooltip content="中间库对应的数据编码字段" placement="top-start" effect="dark">
            <el-form-item label="编码字段" prop="codeField">
              <el-input v-model="form.codeField" placeholder="请输入" :disabled="form.enableFlow==0"/>
            </el-form-item>
            </el-tooltip>
          </el-col>
        </el-row>
        <el-divider content-position="left">更新相关</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="启用更新" prop="enableUpdate">
              <el-switch v-model="form.enableUpdate" :active-value="1" :inactive-value="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新表名" prop="updateTable">
              <el-input v-model="form.updateTable" placeholder="请输入" :disabled="form.enableUpdate==0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新主键" prop="updateKey">
              <el-input v-model="form.updateKey" placeholder="请输入" :disabled="form.enableUpdate==0"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listZjkconfig, getZjkconfig, delZjkconfig, addZjkconfig, updateZjkconfig, exportZjkconfig } from "@/api/aitcommon/zjkconfig";
import {boolFormat} from "@/utils/formater";

export default {
  name: "zjkconfig",
  components: {
  },
  data() {
    return {
      // 表格高度
      tableHeight:0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 中间库配置表格数据
      zjkconfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        code: null,
        name: null,
        sqlConf: null,
        dbConf: null,
        memo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "编码不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.$nextTick(() => {
      // window窗口大小变化后,页面table自适应高度,需要点击 刷新
      // window.innerHeight 浏览器窗口的可见高度，下面的 220 是除了table最大高度的剩余空间。
      let height = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 220;
      this.tableHeight = height;
    })
  },
  methods: {
    // 布尔字段翻译
    boolFormat(row, column) {
      return boolFormat(row,column)
    },
    /** 查询中间库配置列表 */
    getList() {
      this.loading = true;
      listZjkconfig(this.queryParams).then(response => {
        this.zjkconfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加中间库配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      //this.reset();
      const id = row.id || this.ids
      getZjkconfig(id).then(response => {
        console.info('handleUpdate11',response.data.enableFlow)
        this.form = response.data;
        this.open = true;
        this.title = "修改中间库配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateZjkconfig(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZjkconfig(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除中间库配置编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delZjkconfig(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有中间库配置数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportZjkconfig(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => { });
    }
  }
};
</script>
